from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step
from devicetest.log.logger import print_info

from aw.common import common_setup
from aw.pom import Setting, DeskTop, KeyCode, LockScreen


class PreTest_RK3568_Basic_Setting_2100(TestCase, Setting):
    # 预置条件：
    # 1.进入设置-生物识别和密码-密码，点击更改锁屏密码（若无"更改锁屏密码"需要先设置一个锁屏密码）
    # 2.点击输入框，并输入锁屏密码，点击下一步
    # 3.点击〇，输入新的设置锁屏数字密码并再次输入密码
    # 4.设备息屏后重新进入桌面
    # 预期结果：
    # 1.进入输入锁屏密码界面
    # 2.可正常唤起键盘，输入错误密码提示“密码错误，还可尝试(<5)次”；输入正确密码进入设置锁屏数字密码界面
    # 3.可正常唤起键盘，输入新的锁屏密码并再次输入相同密码后会自动返回至生物识别和密码界面
    # 4.需要输入新的锁屏密码才能进入桌面

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：进入设置-生物识别和密码-密码，点击更改锁屏密码（若无"更改锁屏密码"需要先设置一个锁屏密码）')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.touchByText(self.Phone1, '生物识别和密码')
        CommonOH.touchByText(self.Phone1, '锁屏密码')
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.click(self.Phone1, **self.pwd_input_area)
        print_info('预设一个密码')
        # 输入密码123456
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_2)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_3)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_4)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_6)
        CommonOH.wait(self.Phone1, 0.5)
        # 再次输入密码
        Step('步骤2：再次输入锁屏密码123456')
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_2)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_3)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_4)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_6)
        CommonOH.wait(self.Phone1, 0.5)
        Step('预期结果1：进入输入锁屏密码界面')
        CommonOH.wait(self.Phone1, 1)
        CommonOH.checkIfTextExist(self.Phone1, '更改锁屏密码')
        Step('步骤2.1：点击输入框，并输入锁屏密码，点击下一步')
        CommonOH.touchByText(self.Phone1, '更改锁屏密码')
        CommonOH.touchByType(self.Phone1, 'TextInput')
        Step('预期结果2.1：可正常唤起键盘，输入错误密码提示“密码错误，还可尝试(<5)次”；输入正确密码进入设置锁屏数字密码界面')
        CommonOH.checkIfTextExist(self.Phone1, 'space')
        # 输入密码1
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.touchByText(self.Phone1, '下一步')
        CommonOH.checkIfTextExist(self.Phone1, '密码错误，还可尝试 4 次')
        CommonOH.wait(self.Phone1, 0.5)
        Step('步骤2.2：输入正确密码')
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_2)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_3)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_4)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_6)
        CommonOH.touchByText(self.Phone1, '下一步')
        Step('预期结果2.2：进入设置锁屏数字密码界面')
        CommonOH.checkIfTextExist(self.Phone1, '密码由6位数字组成')
        Step('步骤3：点击〇，输入新的设置锁屏数字密码并再次输入密码')
        CommonOH.click(self.Phone1, **self.pwd_input_area)

        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_4)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_6)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_2)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_3)
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_4)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_5)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_6)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_1)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_2)
        CommonOH.sendKeyboardInputEvent(self.Phone1, KeyCode.KEYCODE_3)
        CommonOH.wait(self.Phone1, 0.5)
        Step('预期结果3：自动返回至生物识别和密码界面')
        CommonOH.checkIfTextExist(self.Phone1, '更改锁屏密码')
        CommonOH.goHome(self.Phone1)
        Step('步骤4：设备息屏后重新进入桌面')
        # 熄屏
        CommonOH.shell(self.Phone1, 'power-shell suspend')
        CommonOH.wait(self.Phone1, 0.5)
        # 点亮屏幕
        CommonOH.shell(self.Phone1, 'power-shell wakeup')
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果:4：需要输入锁屏密码才能进入，输入错误密码会提示“密码错误”')
        CommonOH.checkIfTextExist(self.Phone1, '输入密码')
        CommonOH.click(self.Phone1, **LockScreen.NUM_4)
        CommonOH.click(self.Phone1, **LockScreen.NUM_5)
        CommonOH.click(self.Phone1, **LockScreen.NUM_6)
        CommonOH.click(self.Phone1, **LockScreen.NUM_1)
        CommonOH.click(self.Phone1, **LockScreen.NUM_2)
        CommonOH.click(self.Phone1, **LockScreen.NUM_3)
        CommonOH.wait(self.Phone1, 2)
        CommonOH.checkIfTypeExist(self.Phone1, 'Badge')

    def teardown(self):
        Step('收尾1：强制删除/data/*目录并重启，为了清理设置的锁屏密码')
        CommonOH.shell(self.Phone1, 'rm -rf /data/*')
        CommonOH.safeReboot(self.Phone1)
